package com.qingding.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.avos.avoscloud.AVUser;
import com.bumptech.glide.Glide;
import com.qingding.R;
import com.qingding.adapter.MsgAdapter;
import com.qingding.utils.Msg;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

public class ChatActivity extends AppCompatActivity {
    private List<Msg> msgList = new ArrayList<>();
    private EditText inputText;
    private Button send;
    private RecyclerView msgRecyclerView;
    private MsgAdapter adapter;
    private LinearLayoutManager layoutManager;
    private Intent intent;
    private TextView chatFriend;//好友聊天界面上方好友名称
    private ImageView leftImageView ;//左边好友头像
    private ImageView rightImageView ;//右边好友头像（我的头像）
    private TextView chat_time_left;//好友发送的时间
    private TextView chat_time_right;//我发送消息的时间
    private SimpleDateFormat simpleDateFormat;
    private Date date;//当前系统时间

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        intent = getIntent();
        final int leftId = intent.getIntExtra("leftPhotoId",0);
        final int rightId = intent.getIntExtra("rightPhotoId",0);


        simpleDateFormat = new SimpleDateFormat("HH:mm:ss");// HH:mm:ss
        date = new Date(System.currentTimeMillis());
        Msg msg1=new Msg("Hello guy.",Msg.TYPE_RECEIVED,simpleDateFormat.format(date),leftId,rightId);
        msgList.add(msg1);
        Msg msg2=new Msg("Hello.Who is that?",Msg.TYPE_SENT,simpleDateFormat.format(date),leftId,rightId);
        msgList.add(msg2);
        Msg msg3=new Msg("This is Tom!",Msg.TYPE_RECEIVED,simpleDateFormat.format(date),leftId,rightId);
        msgList.add(msg3);




        initMsgs();                                                         //初始化消息数据
        inputText=(EditText)findViewById(R.id.input_text);
        send=(Button)findViewById(R.id.send);
        msgRecyclerView=(RecyclerView)findViewById(R.id.msg_recycler_view);

        String userName = intent.getStringExtra("userName");

        //Log.d("ChatActivity","1515 "+leftId+" "+rightId);
        String objectId = intent.getStringExtra("objectId");
        chatFriend = (TextView) findViewById(R.id.friend_chat_name);//好友名称，显示在标题栏
        leftImageView = (ImageView) findViewById(R.id.image_photo_left);//好友头像，显示在左边
        rightImageView = (ImageView) findViewById(R.id.image_photo_right);//我的头像，显示在右边
        chat_time_left = (TextView) findViewById(R.id.chat_time_left);//好友发送消息的时间
        chat_time_right = (TextView) findViewById(R.id.chat_time_right);//我发送消息的时间


//        AVUser avUser = AVUser.getCurrentUser();
//        rightImageView.setImageResource(rightId);
//        Log.d("1515","1515"+avUser.getInt("PhotoId"));
//        leftImageView.setImageResource(leftId);
        chatFriend.setText(userName);


        layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,true);    //LinearLayoutLayout即线性布局，创建对象后把它设置到RecyclerView当中
        msgRecyclerView.setLayoutManager(layoutManager);
        adapter=new MsgAdapter(msgList);                                    //创建MsgAdapter的实例并将数据传入到MsgAdapter的构造函数中
        msgRecyclerView.setAdapter(adapter);

        send.setOnClickListener(new View.OnClickListener(){                 //发送按钮点击事件
            @Override
            public void onClick(View v){
                String content=inputText.getText().toString();//获取EditText中的内容
                if(!"".equals(content)){//内容不为空则创建一个新的Msg对象，并把它添加到msgList列表中
                    date = new Date(System.currentTimeMillis());
                    Msg msg=new Msg(content,Msg.TYPE_SENT,simpleDateFormat.format(date),leftId,rightId);
                    msgList.add(0,msg);
                    adapter.notifyItemInserted(0);           //调用适配器的notifyItemInserted()用于通知列表有新的数据插入，这样新增的一条消息才能在RecyclerView中显示
                    msgRecyclerView.scrollToPosition(0);     //调用scrollToPosition()方法将显示的数据定位到最后一行，以保证可以看到最后发出的一条消息
                    inputText.setText("");                                  //调用EditText的setText()方法将输入的内容清空
                }
            }
        });
    }

    private void initMsgs(){

    }

//    private String getTime(){
//
//
//        //time1.setText("Date获取当前日期时间"+simpleDateFormat.format(date));
//
//    }


}